﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using Telerik.Web.UI;
using System.Collections.Generic;
using KPIS.GERP.GIMS.DAL;
using KPIS.GERP.GIMS.MODEL;


namespace KPIS.GERP.GIMS.APP.APP_UI.Add_Form
{
    public partial class ICM_Asset_Location : System.Web.UI.Page
    {
        Common.Main cn = new Common.Main();
        private D_Warehouse d_warehouse = new D_Warehouse();
        private D_JobSiteSeq d_job_site = new D_JobSiteSeq();
        private D_AssetLocation d_location = new D_AssetLocation();
        private M_AssetLocation m_location = new M_AssetLocation();
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hLocation_seq.Text = Request.QueryString["location_seq"].ToString();
                hAssetStatus.Text = Request.QueryString["StatusName"].ToString();
                pAssetCode.Text = Request.QueryString["asset_code"].ToString();
                if (hAssetStatus.Text == "Add")
                {
                    ClearObject();
                }
                else if (hAssetStatus.Text == "Edit")
                {
                    LoadData();
                }
            }
        }
        //===================================BUTTON Save Cancel==============================
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (ChkValidate())
            {
                try
                {
                    m_location.asset_code = pAssetCode.Text;
                    m_location.from_job_site_seq = cn.ConvertNullInt(cmbJobSiteFrom.SelectedValue);
                    m_location.from_wh_seq = cn.ConvertNullInt(cmbWarehouseFrom.SelectedValue);
                    m_location.from_other = txtOtherFrom.Text;
                    m_location.to_job_site_seq = cn.ConvertNullInt(cmbJobSiteTo.SelectedValue);
                    m_location.to_wh_seq = cn.ConvertNullInt(cmbWarehouseTo.SelectedValue);
                    m_location.to_other = txtOtherTo.Text;
                    m_location.remark = txtRemark.Text;
                    m_location.location_start_date = Convert.ToDateTime(txtReceiveDate.TextWithLiterals);
                    m_location.location_end_date = cn.ConvertNullDateTime(txtEndDate.TextWithLiterals);
                    m_location.record_status = "U";
                   

                    if (hAssetStatus.Text == "Add")
                    {
                        m_location.created_by = cn.UserID;
                        m_location.created_when = DateTime.Now;
                        d_location.Insert_Asset_Location(m_location);
                        RadAjaxManager1.Alert("เพิ่มข้อมูลเรียบร้อยแล้ว");
                    }
                    else if (hAssetStatus.Text == "Edit")
                    {
                        m_location.location_seq = Convert.ToInt32(hLocation_seq.Text);
                        m_location.updated_by = cn.UserID;
                        m_location.updated_when = DateTime.Now;
                        d_location.Update_Asset_Locaion(m_location);
                        RadAjaxManager1.Alert("แก้ไขข้อมูลเรียบร้อยแล้ว");
                    }
                }
                catch (Exception ex)
                {
                    RadAjaxManager1.Alert("เกิดข้อผิดพลาดระหว่างทำการบันทึกโปรดตรวจสอบ");
                    throw (ex);
                }
                finally
                {
                    RadAjaxManager1.ResponseScripts.Add(@"CloseWindow();");
                }
            }
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            if(hAssetStatus.Text =="Add")
            ClearObject();
            else if (hAssetStatus.Text =="Edit")
            RadAjaxManager1.ResponseScripts.Add(@"CloseWindow();");
        }
        //===================================LOCATION FROM==============================
        protected void cmbWarehouseFrom_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            string strFilter = e.Text;
            IList<MasterWarehouseInfo> infoList = d_warehouse.GetListFillByName(strFilter);
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + 10, infoList.Count);
            e.EndOfItems = endOffset == infoList.Count;

            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbWarehouseFrom.Items.Add(new RadComboBoxItem(infoList[i].Name, infoList[i].Seq.ToString()));
            }

            e.Message = cn.GetStatusMessage(endOffset, infoList.Count);
        }
        protected void cmbJobSiteFrom_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            string strFilter = e.Text;
            IList<M_JobSiteSeq> infoList = d_job_site.GetListFilterByName(strFilter);
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + 10, infoList.Count);
            e.EndOfItems = endOffset == infoList.Count;

            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbJobSiteFrom.Items.Add(new RadComboBoxItem(infoList[i].job_site_name, infoList[i].job_site_seq.ToString()));
            }

            e.Message = cn.GetStatusMessage(endOffset, infoList.Count);
        }
        //===================================LOCATION TO==============================
        protected void cmbJobSiteTo_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            string strFilter = e.Text;
            IList<M_JobSiteSeq> infoList = d_job_site.GetListFilterByName(strFilter);
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + 10, infoList.Count);
            e.EndOfItems = endOffset == infoList.Count;

            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbJobSiteTo.Items.Add(new RadComboBoxItem(infoList[i].job_site_name, infoList[i].job_site_seq.ToString()));
            }

            e.Message = cn.GetStatusMessage(endOffset, infoList.Count);
        }
        protected void cmbWarehouseTo_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            string strFilter = e.Text;
            IList<MasterWarehouseInfo> infoList = d_warehouse.GetListFillByName(strFilter);
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + 10, infoList.Count);
            e.EndOfItems = endOffset == infoList.Count;

            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbWarehouseTo.Items.Add(new RadComboBoxItem(infoList[i].Name, infoList[i].Seq.ToString()));
            }

            e.Message = cn.GetStatusMessage(endOffset, infoList.Count);
        }
        private static string GetStatusMessage(int offset, int total)
        {
            if (total <= 0)
                return "ไม่มีข้อมูล";

            return String.Format("รายการที่ <b>1</b>-<b>{0}</b> จากทั้งหมด <b>{1}</b> รายการ", offset, total);
        }
        //===================================Function Utility==============================
        private void ClearObject()
        {
            txtReceiveDate.Text = "";
            txtEndDate.Text = "";
            cmbJobSiteFrom.ClearSelection();
            cmbJobSiteFrom.Text = "";
            cmbWarehouseFrom.ClearSelection();
            cmbWarehouseFrom.Text = "";
            txtOtherFrom.Text = "";
            cmbJobSiteTo.ClearSelection();
            cmbJobSiteTo.Text = "";
            cmbWarehouseTo.ClearSelection();
            cmbWarehouseTo.Text = "";
            txtOtherTo.Text = "";
            txtRemark.Text = "";
        }
        private void EnableObject(bool pFlag)
        {
            txtReceiveDate.Enabled = pFlag;
            txtEndDate.Enabled = pFlag;
            cmbJobSiteFrom.Enabled = pFlag;
            cmbWarehouseFrom.Enabled = pFlag;
            txtOtherFrom.Enabled = pFlag;
            cmbJobSiteTo.Enabled = pFlag;
            cmbWarehouseTo.Enabled = pFlag;
            txtOtherTo.Enabled = pFlag;
            txtRemark.Enabled = pFlag;
        }
        private bool ChkValidate()
        {
            bool pFlag = true;
            if (txtReceiveDate.Text == "")
            {
                pFlag = false;
                RadAjaxManager1.Alert("กรุณากรอกวันที่รับเข้าด้วย");
                txtReceiveDate.Focus();
            }
            else if ((cmbJobSiteFrom.SelectedValue=="")&& (cmbWarehouseFrom.SelectedValue=="") && (txtOtherFrom.Text ==""))
            {
                pFlag =false;
                RadAjaxManager1.Alert("กรุณาเลือกสถานที่ก่อนที่จะรับเข้าด้วย");
            }
            else if ((cmbJobSiteTo.SelectedValue == "") && (cmbWarehouseTo.SelectedValue == "") && (txtOtherTo.Text == ""))
            {
                pFlag = false;
                RadAjaxManager1.Alert("กรุณาเลือกสถานที่เก็บพัสดุปัจจุบันด้วย");
            }
            return pFlag;
        }
        private void LoadData()
        {
            M_AssetLocation info=d_location.GetListFillBySeq(hLocation_seq.Text);
            txtReceiveDate.Text = cn.ConvertStrToStrDate(info.location_start_date.ToString());
            txtEndDate.Text = cn.ConvertStrToStrDate(info.location_end_date.ToString());
            cmbJobSiteFrom.SelectedValue = info.from_job_site_seq.ToString();
            cmbJobSiteFrom.Text = info.job_site_from_name.ToString();
            cmbWarehouseFrom.SelectedValue = info.from_wh_seq.ToString();
            cmbWarehouseFrom.Text = info.wh_from_name.ToString();
            txtOtherFrom.Text = info.from_other.ToString();
            cmbJobSiteTo.SelectedValue = info.to_job_site_seq.ToString();
            cmbJobSiteTo.Text = info.job_site_to_name.ToString();
            cmbWarehouseTo.SelectedValue = info.to_wh_seq.ToString();
            cmbWarehouseTo.Text = info.wh_to_name.ToString();
            txtOtherTo.Text = info.to_other.ToString();
            txtRemark.Text = info.remark.ToString();
        }
        //======================Control UI=====================//
        //private void UpdateUILocation(string strCommand)
        //{
        //    switch (strCommand)
        //    {
        //        case "Load":
        //            EnableObjecTabReceieve(true);
        //            ClearObjectTabReceive();
        //            btnInsertReceive.Enabled = true;
        //            btnSaveReceive.Enabled = false;
        //            btnCancelReceive.Enabled = false;
        //            break;
        //        case "Insert":
        //            EnableObjecTabReceieve(false);
        //            ClearObjectTabReceive();
        //            btnInsertReceive.Enabled = false;
        //            btnSaveReceive.Enabled = true;
        //            btnCancelReceive.Enabled = true;
        //            break;
        //        case "Edit":
        //            EnableObjecTabReceieve(true);
        //            btnInsertReceive.Enabled = false;
        //            btnSaveReceive.Enabled = true;
        //            btnCancelReceive.Enabled = true;
        //            break;
        //        case "Save":
        //            EnableObjecTabReceieve(false);
        //            btnInsertReceive.Enabled = true;
        //            btnSaveReceive.Enabled = false;
        //            btnCancelReceive.Enabled = false;
        //            break;
        //        case "Cancel":
        //            EnableObjecTabReceieve(true);
        //            ClearObjectTabReceive();
        //            btnInsertReceive.Enabled = true;
        //            btnSaveReceive.Enabled = false;
        //            btnCancelReceive.Enabled = false;
        //            break;
        //    }
        //}
    }
}
